extends Area2D
class_name Reward

# 道具类型枚举
enum RewardType {
	LIFE,      # 生命
	ARMOR,     # 护甲
	LEVEL,     # 升级
	BOOM,      # 炸弹
	PROTECTION # 保护
}

# 道具属性
var reward_type: RewardType = RewardType.LIFE

# 道具纹理
var reward_textures = {
	RewardType.LIFE: preload("res://assets/Images/Reward/Life.png"),
	RewardType.ARMOR: preload("res://assets/Images/Reward/Armor.png"),
	RewardType.LEVEL: preload("res://assets/Images/Reward/Level.png"),
	RewardType.BOOM: preload("res://assets/Images/Reward/Boom.png"),
	RewardType.PROTECTION: preload("res://assets/Images/Reward/Protection.png")
}

# 节点引用
@onready var sprite = $Sprite2D
@onready var collision_shape = $CollisionShape2D
@onready var timer = $Timer

# 信号
signal reward_collected(reward_type: RewardType)

func _ready():
	# 随机选择道具类型
	reward_type = RewardType.values()[randi() % RewardType.size()]
	
	# 设置纹理
	update_sprite()
	
	# 设置碰撞形状
	var shape = RectangleShape2D.new()
	shape.size = Vector2(25, 25)
	collision_shape.shape = shape
	
	# 连接信号
	area_entered.connect(_on_area_entered)
	body_entered.connect(_on_body_entered)
	timer.timeout.connect(_on_timer_timeout)

func update_sprite():
	if reward_type in reward_textures:
		sprite.texture = reward_textures[reward_type]

func _on_area_entered(area):
	# 处理与玩家的碰撞
	if area.get_parent() is Player:
		collect_reward()

func _on_body_entered(body):
	# 处理与玩家的碰撞
	if body is Player:
		collect_reward()

func collect_reward():
	# 发送收集信号
	reward_collected.emit(reward_type)
	
	# 播放收集音效
	play_collect_sound()
	
	# 移除道具
	queue_free()

func play_collect_sound():
	# 播放道具收集音效 - 使用简单音效
	print("道具收集音效")

func _on_timer_timeout():
	# 道具超时消失
	queue_free()
